<?php
/**
 * Created by Engineer CuiLiwu.
 * Project: deal.
 * Date: 2018/6/21-8:57
 * License Hangzhou orce Technology Co., Ltd. Copyright © 2018
 */

namespace App\Http\Controllers\Web\Qualified;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class QualifiedController extends BaseController
{
    protected $api_url = [
        'show'     => ['get', 'qualified'],       // 按条件 获取含资质服务商品的店铺列表
        'products' => ['get', 'qualified/products'], // 按条件 获取资质商品列表
        'getCate'  => ['post', 'qualified/getCate'], // 按条件 获取资质的分类列表
        'getCateLevel' => ['post', 'qualified/getCateLevel/%d'],// 按条件 获取所有资质等级 attribute_id 为 151[已指定]
    ];

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }
    /**
     *  跳转首页
     * */
    public function index()
    {
        return redirect('/');
    }
    public function show()
    {

        //参数部分
        $search_params = $this->request->all(['third_cat','area','price']);

        $page = $this->request->get('page', 1);

        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'page' => $page
        ];

        // 请求部分 - 查询出指定分类的商品 并获取 shop_id
        $ret = http_web($this->api_url['products'])->send($params);
        $shop_ids = implode(',',array_unique(array_filter(array_column($ret['data'],'shop_id'))));

        // 查询店铺列表条件
        $search_params = [ // 此数组已重置
            'sp_id' => $shop_ids,
        ];
        $sParams = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'with'=>'companyMaterial',
            'page' => $page
        ];
        // 请求部分 - 根据 shop_id 查询有此服务的店铺列表
        $shop = http_web($this->api_url['show'])->send($sParams);
        $data = paginate($this->request, $shop['data']);

        // 请求部分 - 查询出指定的服务分类。
        $servCate = \Cache::remember('cate_qualified',0,function(){
            return http_web($this->api_url['getCate'])->send();
        });

        // 值
        $data['catelist'] = $servCate['data'];  // 服务分类列表
        $data['cateArr']  = array_column($servCate['data'],'id');
        $data['current']  = [
            'currentCate' => $this->request->get('third_cat'),
        ];  // 服务分类列表

        return view('web.find.aptitude', $data);
    }


}